package com.crowntit.domain.activity.service.discount;

import com.crowntit.domain.activity.adapter.repository.IActivityRepository;
import com.crowntit.domain.activity.model.valobj.DiscountTypeEnum;
import com.crowntit.domain.activity.model.valobj.GroupBuyActivityDiscountVO;

import javax.annotation.Resource;
import java.math.BigDecimal;

public abstract class AbstractDiscountCalculateService implements IDiscountCalculateService{

    @Resource
    private IActivityRepository repository;

    @Override
    public BigDecimal calculate(String userId, BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount){
        if(DiscountTypeEnum.TAG.equals(groupBuyDiscount.getDiscountType())){
            boolean isCrowdRange = filter(userId, groupBuyDiscount.getTagId());
            if (!isCrowdRange){
                return originalPrice;
            }
        }

        BigDecimal calculatedPrice = doCalculate(originalPrice, groupBuyDiscount);
        if (calculatedPrice.compareTo(BigDecimal.ZERO) <= 0){
            return new BigDecimal("0.01");
        }
        return calculatedPrice;
    }

    public boolean filter(String userId, String tagId){
        return repository.isCrowdRange(userId, tagId);
    }

    public abstract BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount);
}
